package com.dmgis.hndz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dmgis.hndz.common.utils.TreeUtil;
import com.dmgis.hndz.mapper.Ggsy01aMapper;
import com.dmgis.hndz.mapper.Ggsy02aMapper;
import com.dmgis.hndz.pojo.*;
import com.dmgis.hndz.pojoVo.Ggsy02bVO;
import com.dmgis.hndz.service.Ggsy02bService;
import com.dmgis.hndz.service.MessageSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MessageSendServiceImpl implements MessageSendService {

    @Autowired
    private Ggsy01aMapper ggsy01aMapper;

    @Autowired
    private Ggsy02aMapper ggsy02aMapper;

    @Autowired
    private Ggsy02bService ggsy02bService;


    @Override
    public List<AreaTree> getTree(String advcode) {
        List<AreaTree> list = new ArrayList<>();
        List<Ggsy01a> arealist = new ArrayList<>();
        List<Ggsy02a> deptlist = new ArrayList<>();
        if (advcode.endsWith("0000")) {
            arealist = ggsy01aMapper.getProXzdy();
            deptlist = ggsy02aMapper.getProDept();
        } else if (advcode.endsWith("00")) {
            arealist = ggsy01aMapper.getCityXzdy();
            deptlist = ggsy02aMapper.getCityDept();
        } else {
            arealist = ggsy01aMapper.getCountyXzdy();
            deptlist = ggsy02aMapper.getCountyDept();
        }


        //行政区划树
        List<AreaTree> treeList = arealist.stream()
                .filter(a -> !a.getGgsy01a010().equals(a.getGgsy01a040()))
                .map(a -> {
                    AreaTree node = new AreaTree();
                    node.setId((long) a.getGgsy01a010());//id
                    node.setStrId(node.getId().toString());
                    node.setParentId((long) a.getGgsy01a040());//parentid
                    node.setName(a.getGgsy01a030());//name
                    node.setCode(a.getGgsy01a020());//code
                    node.setDisabled(true);
                    return node;
                }).collect(Collectors.toList());

        // 权限内部门
        List<DeptTree> collect = deptlist.stream().filter(dept -> !dept.getGgsy02a010().equals(dept.getGgsy02a070()))
                .map(dept -> {
                    DeptTree node = new DeptTree();
                    node.setId(dept.getGgsy02a010());
                    node.setStrId(dept.getGgsy02a010().toString());
                    node.setSort(dept.getGgsy02a030());
                    node.setParentId(dept.getGgsy02a070());
                    node.setName(dept.getGgsy02a020());
                    node.setAreaCode(dept.getGgsy02a090());
                    node.setUnitType(dept.getGgsy02a110());
                    return node;
                }).sorted((dept1, dept2) -> {
                            return dept1.getSort().compareTo(dept2.getSort());
                        }
                ).collect(Collectors.toList());
        TreeUtil treeUtil = new TreeUtil();
        if (advcode.endsWith("0000")) {
            return treeUtil.build(treeList, collect, 1L);
        } else if (advcode.endsWith("00")) {
            return treeUtil.build(treeList, collect, Long.parseLong(advcode.substring(0, 4)));
        } else {
            return treeUtil.build(treeList, collect, Long.parseLong(advcode));
        }
    }




    public List<AreaTree> getAllPersonsTree(String advcode) {
        List<Ggsy02bVO> personList = ggsy02bService.selectGgsy02bList(null); // 获取所有人员信息
        List<Ggsy01a> arealist = new ArrayList<>();
        List<Ggsy02a> deptlist = new ArrayList<>();

        if (advcode.endsWith("0000")) {
            arealist = ggsy01aMapper.getProXzdy();
            deptlist = ggsy02aMapper.getProDept();
//        } else if (advcode.endsWith("00")) {
//            arealist = ggsy01aMapper.getCityXzdy();
//            deptlist = ggsy02aMapper.getCityDept();
//        } else {
//            arealist = ggsy01aMapper.getCountyXzdy();
//            deptlist = ggsy02aMapper.getCountyDept();
//        }
        } else if (advcode.endsWith("00")) {
            String code = advcode.substring(0, 4);
            arealist = ggsy01aMapper.getCityOrCountyXzdy(code);
            deptlist = ggsy02aMapper.getCityOrCountyDept(code);
        }
        else{
            arealist = ggsy01aMapper.getCityOrCountyXzdy(advcode);
            deptlist = ggsy02aMapper.getCityOrCountyDept(advcode);
        }
        // 行政区划树
        List<AreaTree> treeList = arealist.stream()
                .filter(a -> !a.getGgsy01a010().equals(a.getGgsy01a040()))
                .map(a -> {
                    AreaTree node = new AreaTree();
                    node.setId(a.getGgsy01a010()); // id
                    node.setStrId(node.getId().toString());
                    node.setParentId(a.getGgsy01a040()); // parentid
                    node.setName(a.getGgsy01a030()); // name
                    node.setCode(a.getGgsy01a020()); // code
                    node.setDisabled(true);
                    return node;
                }).collect(Collectors.toList());

        // 权限内部门
        List<DeptTree> deptTreeList = deptlist.stream()
                .filter(dept -> !dept.getGgsy02a010().equals(dept.getGgsy02a070()))
                .map(dept -> {
                    DeptTree node = new DeptTree();
                    node.setId(dept.getGgsy02a010());
                    node.setStrId(dept.getGgsy02a010().toString());
                    node.setSort(dept.getGgsy02a030());
                    node.setParentId(dept.getGgsy02a070());
                    node.setName(dept.getGgsy02a020());
                    node.setAreaCode(dept.getGgsy02a090());
                    node.setUnitType(dept.getGgsy02a110());
                    return node;
                }).sorted(Comparator.comparing(DeptTree::getSort))
                .collect(Collectors.toList());

        // 构建 PersonTree 列表
        List<PersonTree> personTreeList = personList.stream().map(person -> {
            PersonTree personNode = new PersonTree();
            personNode.setPersonId(person.getGgsy02b010().toString());
            personNode.setName(person.getGgsy02b020());
            personNode.setPhone(person.getGgsy02b080());
            personNode.setDept(person.getGgsy02a020());
            personNode.setDeptId(person.getGgsy02b015().toString());
            return personNode;
        }).collect(Collectors.toList());

        TreeUtil treeUtil = new TreeUtil();
        if (advcode.endsWith("0000")) {
            return treeUtil.build(treeList, deptTreeList, 1L, personTreeList);
        } else if (advcode.endsWith("00")) {
            return treeUtil.build(treeList, deptTreeList, Long.parseLong(advcode.substring(0, 4)), personTreeList);
        } else {
            return treeUtil.build(treeList, deptTreeList, Long.parseLong(advcode), personTreeList);
        }
    }

    @Override
    public boolean sys_dept_add(Ggsy02a sysDept) {
        sysDept.setGgsy02a040(Timestamp.valueOf(LocalDateTime.now()));
        int flag = ggsy02aMapper.insert(sysDept);
        return flag > 0 ? true : false;
    }

    /**
     * 根据部门id查询部门标准信息
     *
     * @param id  部门id
     * @return
     */
    @Override
    public Ggsy02a sys_dept_get(String id) {
        return ggsy02aMapper.selectById(id);
    }

    /**
     * 修改部门标准信息
     *
     * @param sysDept 部门信息对象
     * @return
     */
    @Override
    public boolean sys_dept_update(Ggsy02a sysDept) {
        sysDept.setGgsy02a050(Timestamp.valueOf(LocalDateTime.now()));
        int flag = ggsy02aMapper.updateById(sysDept);
        return flag > 0 ? true : false;
    }

    /**
     * 删除部门信息
     *
     * @param id 部门id
     * @return
     */
    @Override
    public boolean sys_dept_delete(String id) {
        int flag = ggsy02aMapper.deleteById(id);
        return flag > 0 ? true : false;
    }


    /**
     * 查询行政区
     *
     * @param type  地区类型：省1，市2，区县3，乡镇4，村5
     * @param parentId 父地区id
     * @return
     */
    @Override
    public List<Ggsy01a> getRegion(String type, Long parentId) {
        List<Ggsy01a> region = ggsy01aMapper.getRegion(type, parentId);
        return region;
    }


}
